﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class D_Treatment_Product
    {
        public bool CreateOneTreatment_Product(Treatment_Product_Info objTreatment_Product_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Treatment_Product_Insert", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"DairyProductID", objTreatment_Product_Info.DairyProductID));
            command.Parameters.Add(new SqlParameter(@"MixTreatmentID", objTreatment_Product_Info.MixTreatmentID));
            command.Parameters.Add(new SqlParameter(@"Quanlity", objTreatment_Product_Info.Quanlity));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //**********************************************************************************       
        public bool EditOneTreatment_Product(Treatment_Product_Info objTreatment_Product_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Treatment_Product_Update", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DairyProductID", objTreatment_Product_Info.DairyProductID));
            command.Parameters.Add(new SqlParameter("@MixTreatmentID", objTreatment_Product_Info.MixTreatmentID));
            command.Parameters.Add(new SqlParameter("@Quanlity", objTreatment_Product_Info.Quanlity));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public bool RemoveOneTreatment_Product(int DairyProductID, int MixTreatmentID)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Treatment_Product_Delete", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"DairyProductID", DairyProductID));
            command.Parameters.Add(new SqlParameter(@"MixTreatmentID", MixTreatmentID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public DataTable GetAllTreatment_Product()
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Treatment_Product_SelectAll", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public Treatment_Product_Info GetOneTreatment_Product(int DairyProductID)
        {
            Treatment_Product_Info objTreatment_Product_Info = new Treatment_Product_Info();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Treatment_Product_SelectByID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"DairyProductID", DairyProductID));
            try
            {
                objConnection.Open();
                SqlDataReader ordTreatment_Product = command.ExecuteReader(); //OjectRecordDatareader
                ordTreatment_Product.Read();
                if (ordTreatment_Product.HasRows)
                {
                    if (ordTreatment_Product["DairyProductID"] != DBNull.Value)
                    {
                        objTreatment_Product_Info.DairyProductID = Convert.ToInt16(ordTreatment_Product["DairyProductID"]);
                    }
                    if (ordTreatment_Product["MixTreatmentID"] != DBNull.Value)
                    {
                        objTreatment_Product_Info.MixTreatmentID = Convert.ToInt16(ordTreatment_Product["MixTreatmentID"]);
                    }
                    if (ordTreatment_Product["Quanlity"] != DBNull.Value)
                    {
                        objTreatment_Product_Info.Quanlity = Convert.ToInt16(ordTreatment_Product["Quanlity"]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return objTreatment_Product_Info;
        }
    }
}
